package com.inspur.cloud.controller.resource;

import com.inspur.cloud.entity.dataspace.UserEntity;
import com.inspur.cloud.entity.dataspace.ao.resource.DistributeAO;
import com.inspur.cloud.entity.dataspace.common.OpEntityResult;
import com.inspur.cloud.entity.dataspace.common.PageReqParam;
import com.inspur.cloud.entity.dataspace.common.PageResult;
import com.inspur.cloud.entity.dataspace.vo.HiveAddVO;
import com.inspur.cloud.entity.dataspace.vo.HiveReq;
import com.inspur.cloud.entity.dataspace.vo.HiveVO;
import com.inspur.cloud.security.AuthorizationHelper;
import com.inspur.cloud.service.dataspace.HiveManagerService;
import com.inspur.cloud.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.security.RolesAllowed;

@RestController
@RequestMapping("/dataspace/api/v1/hive")
@Api(value = "hive", description = "hive资源管理")
public class HiveManagerController {
    @Autowired
    HiveManagerService hiveManagerService;

    @RequestMapping(value = "/list", method = {RequestMethod.POST})
    @ApiOperation(value = "资源列表", notes = "")
    public PageResult<HiveVO> showPublic(@ApiParam(value = "hive查询对象")
                                          @RequestBody PageReqParam<HiveReq> hiveReq) {
        PageResult<HiveVO> result = new PageResult<>();
        if (hiveReq.getReqParam() == null) {
            result.setCode(-1);
            result.setMsg("参数不完整");
            return result;
        }
        if (hiveReq.getPageSize() > 50 || hiveReq.getPageSize() < 0) {
            result.setCode(-1);
            result.setMsg("页码非法");
            return result;
        }
        String resourceName=hiveReq.getReqParam().getResourceName();
        if(!StringUtil.isEmpty(resourceName)){//过滤特殊字符，解决搜索问题
            resourceName=StringUtil.specialStr(resourceName);
            hiveReq.getReqParam().setResourceName(resourceName);
        }
        result = hiveManagerService.getList(hiveReq);
        return result;
    }

    @PostMapping("/addRes")
    @ApiOperation(value = "新建HIVE资源，返回数据库里的资源id", notes = "只支持新建数据库；只允许管理员调用")
    @RolesAllowed("Admin")
    public OpEntityResult<Integer> addRes(@ApiParam(value = "HIVE传参")
                                          @RequestBody HiveAddVO hiveVO) throws Exception {
        hiveVO.setParentId(0);//保证进入service后parentId不为null
        if(StringUtil.isEmpty(hiveVO.getDatabaseName())){
            return OpEntityResult.fail("数据库名不能为空");
        }
        if(hiveVO.getDatabaseName().startsWith("_")){
            return OpEntityResult.fail("数据库名不能以下划线(_)开头");
        }
        UserEntity toUser=AuthorizationHelper.getUser();
        OpEntityResult<Integer> result = hiveManagerService.addRes(hiveVO,toUser);
        return result;
    }

    @PostMapping("/distributeRes")
    @ApiOperation(value = "分配HIVE资源，返回数据库里的资源id", notes = "")
    public OpEntityResult<Integer> distributeRes(@ApiParam(value = "HIVE传参")
                                                 @RequestBody DistributeAO hiveVO) throws Exception {
        OpEntityResult<Integer> result = new OpEntityResult();
        if (hiveVO.getResourceId() == null) {
            return OpEntityResult.fail("资源ID 不可为空");
        }
        return hiveManagerService.distributeRes(hiveVO);
    }

    @PostMapping("/shareRes")
    @ApiOperation(value = "共享HIVE资源，返回数据库里的资源id", notes = "")
    public OpEntityResult<Integer> share(@ApiParam(value = "HIVE传参")
                                         @RequestBody DistributeAO hiveVO) throws Exception {
        OpEntityResult<Integer> result = new OpEntityResult();
        if (hiveVO.getResourceId() == null) {
            return OpEntityResult.fail("资源ID 不可为空");
        }
        return hiveManagerService.share(hiveVO);
    }

    @DeleteMapping("/delRes")
    @ApiOperation(value = "删除hive资源", notes = "")
    @RolesAllowed({"Admin", "Tenant"})
    public OpEntityResult<Integer> delRes(@ApiParam(value = "资源ID")
                                          @RequestParam(value = "resourceId" ,required = true) Integer resourceId,
                                          @ApiParam(value = "是否保留数据，1保留0不保留")
                                          @RequestParam(value = "keepData", required = true) Integer keepData) {
        OpEntityResult<Integer> result = new OpEntityResult();
        if(!AuthorizationHelper.isAdmin() && !AuthorizationHelper.isTenant()){
            result.setCode(-1);
            result.setMsg("非租户及管理员没有删除资源的权限");
            return result;
        }
        return hiveManagerService.delRes(resourceId, keepData);
    }

    @GetMapping("/sync")
    @ApiOperation(value = "同步hive资源", notes = "同步hive的表和列，返回同步的资源数")
    @RolesAllowed({"Admin", "Tenant"})
    public OpEntityResult<Integer> delRes(@ApiParam(value = "资源ID")
                                          @RequestParam(value = "resourceId", required = true) Integer resourceId,
                                          @ApiParam(value = "资源名，预留字段")
                                          @RequestParam(value = "resourceName", required = false) String resourceName) {
        OpEntityResult<Integer> result = new OpEntityResult();
        if (resourceId == 0 && !AuthorizationHelper.isAdmin()) {
            return OpEntityResult.fail("非管理员不能同步库级数据");
        }
        return hiveManagerService.sync(resourceId, resourceName);
    }
}
